﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATEGo
{
    //———————————————————————【2获取Running相关的变量值】————————————————————————
    public partial class Executor
    {
        //———————————————————————目前正在执行的变量Running1————————————————————————
        /// <summary>
        /// 当前执行flow的所有item，要求每一项为 "[命令集名称]:cmdTag"
        /// </summary>
        private List<string> Running_Flows { get; set; } = new List<string>();
        /// <summary>
        /// 正在执行的Order
        /// </summary>
        private OrderItem Running_OrderItem { get; set; }
        /// <summary>
        /// 正在执行的，即默认的CmdSuits
        /// </summary>
        private CmdSuits Running_CmdSuits { get; set; }


        /// <summary>
        /// 将每一个命令Cmdtag恢复成完整的名称，如果是self的则继续解析成真正的CmdTag
        /// </summary>
        /// <param name="thisOrder"></param>
        private void RecoverCmdTagRealName(OrderItem thisOrder)
        {
            if (thisOrder.defaultCmdSuit.Trim().ToLower() == "self")
            {
                foreach (var item in thisOrder.items)
                {
                    //直接项 self的继续解析
                    if (!item.Contains(":"))
                    {
                        //以$开头不需要加了
                        if (item.StartsWith(Name.CmdSuits_Base))
                        {
                            Running_Flows.Add(item);
                        }
                        //跳到对应的继续解析
                        else if (Initial.AllOrderItems.Keys.Contains(item))
                        {
                            RecoverCmdTagRealName(Initial.AllOrderItems[item]);
                        }
                        //都不满足，则不解析了
                    }
                    //间接项 [SSH]:Pause_5
                    else
                    {
                        Running_Flows.Add(item);
                    }
                }
            }
            else
            {
                foreach (var item in thisOrder.items)
                {
                    //直接项 Pause_5 -> [SSH]:Pause_5
                    if (!item.Contains(":"))
                    {
                        //以$开头不需要加了
                        if (item.StartsWith(Name.CmdSuits_Base))
                        {
                            Running_Flows.Add(item);
                        }
                        else
                        {
                            Running_Flows.Add("[" + thisOrder.defaultCmdSuit.Trim() + "]:" + item);
                        }
                    }
                    //间接项 [SSH]:Pause_5
                    else
                    {
                        //self的继续解析 [self]:Telnet -> [SSH]:Pause_5 
                        if (item.ToLower().StartsWith("[self]"))
                        {
                            var t = SplitCmdTag(item);
                            if (Initial.AllOrderItems.Keys.Contains(t[1]))
                            {
                                RecoverCmdTagRealName(Initial.AllOrderItems[t[1]]);
                            }
                        }
                        //[SSH]:Pause_5
                        else
                        {
                            Running_Flows.Add(item);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 将命令集地址名称[0]和cmdtag[1]拆分出来
        /// 间接项 [SSH]:Pause_5 拆出来
        /// </summary>
        /// <param name="cmdTag"></param>
        /// <returns></returns>
        private string[] SplitCmdTag(string cmdTag)
        {
            cmdTag = cmdTag.Trim();
            //拆$Cmd_Restart
            if (cmdTag.StartsWith(Name.CmdSuits_Base))
            {
                return new string[] { Name.CmdSuits_Base, cmdTag };
            }
            else //拆[SSH]:Pause_5
            {
                return cmdTag.Split(new string[] { ":", "[", "]" }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

        /// <summary>
        /// 获取Running相关的变量值
        /// </summary>
        /// <param name="ExeFlowName"></param>
        private bool P2_GetRunningParams(string ExeFlowName)
        {
            //获取到命令集中的指定Order
            Running_OrderItem = Initial.AllOrderItems[ExeFlowName];
            //解析cmdtag名称成符合的格式，放入Running_Flows中
            Running_Flows.Clear();
            RecoverCmdTagRealName(Running_OrderItem);
            if (Running_OrderItem is null)
            {
                return false;
            }
            return true;
        }
    }
}
